'use strict';

/* httpPlus Service */
app.service('httpPlus',
    ['$http', '$window', '$q', 'maskPlus', 'utils','testData','$stateParams',
    function ($http, $window, $q, maskPlus, utils, testData,$stateParams) {
    console.log($stateParams);
    var ajax = function (_config) {
        maskPlus.showLoading();
        var ajaxPromise = $.ajax(_config);
        var deferred = $q.defer();
        console.log('- req', _config.url);
        console.log('     ', _config.data);
        ajaxPromise.then(function (_data) {
            console.log('- res', _data);
            if (_data.Result == undefined) {
                deferred.resolve(_data);
            } else {
                if (_data.Result){
                    deferred.resolve(_data.ResultData);
                    maskPlus.hideLoading();
                }
                else
                    deferred.reject(_data);
            }

        }, function (data) {
            deferred.reject(data);
        }, function (data) {
            deferred.notify(data);
        });
        return deferred.promise;
    };
    var ajaxTestData = function (_exec, _params) {
        var path = _exec.replace(/\//g, '.');
        console.log(path);
        var data = _.get(testData, path);
        if(data == undefined){
            data = { Result: false, ResultMsg: 'test data not found.'};
        }else if(typeof(data) == 'function'){
            //noinspection JSUnresolvedFunction
            data = data(_params);
        }
        console.log('- req', _exec);
        maskPlus.showLoading();
        var deferred = $q.defer();
        setTimeout(function () {
            console.log('- res', data);
            if (data.Result) {
                maskPlus.hideLoading();
                deferred.resolve(data.ResultData);
            }else {
                maskPlus.hideLoading();
                deferred.reject(data.ResultMsg);
            }
        }, 500);
        return deferred.promise;
    };
    // _exec = 'testData.menuDetails'
    this.MMB2RRRApi =  function (_exec, _data,type) {
        console.log(type);
        maskPlus.showLoading();
        if (window.location.port == '3004') {
            return ajaxTestData(_exec, _data);
        } else {
            // url
            var url = '';
            if (window.location.port == '62643' || window.location.port == '13596' || window.location.port == '3006') {
                if(type == undefined){
                    url =  'http://localhost:13596/ToFuTest/Api/RedirectMMB.ashx';
                    url += '?exec=' + _exec + '&prefix=MMB2BLL/';
                }else{
                    url = 'http://localhost:13596/ToFuTest/Api/RedirectMMB.ashx';
                    url += '?exec=' + _exec;
                }

                console.log('res', _data);
            }  else {
                if(type == undefined){
                    url = utils.wechatSiteName + '/Api/RedirectMMB.ashx';
                    url += '?exec=' + _exec + '&prefix=MMB2BLL/';
                }else {
                    url = utils.wechatSiteName + '/Api/RedirectMMB.ashx';
                    url += '?exec=' + _exec;
                }

            }
            // config
            var config = {
                url: url,
                type: 'POST',
                cache: false,
                contentType: false,
                processData: false,
                data: new FormData()
            };
            // data
            _data = _data || {};

            for (var key in _data) {
                var value = _data[key];
                if (value == undefined || value == null) continue;
                if (typeof (value) == 'boolean' || typeof (value) == 'string' || typeof (value) == 'number' ||
                    value instanceof Boolean || value instanceof String || value instanceof Number || value instanceof File)
                    config.data.append(key, value);
                else
                    config.data.append(key, JSON.stringify(value));
            }

            //for (var key in _data) {
            //    if (_data[key] == undefined) continue;
            //    if (typeof _data[key] == 'object')
            //        config.data.append(key, JSON.stringify(_data[key]));
            //    else
            //        config.data.append(key, _data[key]);
            //}
            return ajax(config);
        }
    };
    this.RRRApi = function (_exec, _data) {
        maskPlus.showLoading();
        if (window.location.port == '3006' || window.location.port == '13596') {
            return ajaxTestData(_exec, _data);
        } else {
            // url
            var url = '';
            if (window.location.port == '62643' || window.location.port == '13596' || window.location.port == '3005') {
                url = 'http://localhost:13596/ToFuTest/Api/RedirectMMB.ashx';
            } else {
                url = utils.wechatSiteName + '/Api/RedirectMMB.ashx';
            }
            // 1
            url += '?exec=WechatPage/' + _exec;

            // config
            var config = {
                url: url,
                type: 'POST',
                cache: false,
                contentType: false,
                processData: false,
                data: new FormData()
            };
            // data
            _data = _data || {};
            for (var key in _data) {
                if (_data[key] == undefined) continue;
                if (typeof _data[key] == 'object')
                    config.data.append(key, JSON.stringify(_data[key]));
                else
                    config.data.append(key, _data[key]);
            }

            return ajax(config);
        }
    };
    this.ServerIndex = function (_exec, _data) {
        return this.RRRApi('ServerIndexBLL/' + _exec, _data);
    };

    }]);